package one.three.five;

import java.util.Iterator;
import java.util.NoSuchElementException;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;

public class Binary {

	static class Stack<Item> {

		private Node<Item> first;
		private int n;

		public Stack() {
			first = null;
			n = 0;
		}

		public boolean isEmpty() {
			return first == null;
		}

		public int size() {
			return n;
		}

		public void push(Item item) {
			Node oldfirst = first;
			first = new Node();
			first.item = item;
			first.next = oldfirst;
			n++;
		}

		public Item pop() {
			if (isEmpty()) {
				throw new NoSuchElementException("Stack underflow");
			}
			Item item = (Item) first.item;
			first = first.next;
			n--;
			return item;
		}

		private class Node<Item> {
			Item item;
			Node<Item> next;
		}

		private class LinkedIterator<Item> implements Iterator<Item> {
			private Node<Item> current;

			public LinkedIterator(Node<Item> first) {
				this.current = first;
			}

			@Override
			public boolean hasNext() {

				return current != null;
			}

			@Override
			public void remove() {
				throw new UnsupportedOperationException();
			}

			@Override
			public Item next() {
				if (!hasNext()) {
					throw new NoSuchElementException();
				}
				Item item = current.item;
				current = current.next;
				return item;
			}

		}

	}

	public static void main(String[] args) {
		int n = 50;
		Stack<Integer> s = new Stack<Integer>();
		while (n > 0) {
			s.push(n % 2);
			n = n / 2;
		}
		while(!s.isEmpty())
			StdOut.print(s.pop());
		
	}

}
